package com.ewiqi.springsecuritydemo.service;

import com.ewiqi.springsecuritydemo.model.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

/**
 * @author fengyu.zhang
 * @date 2021/1/18 7:14 下午
 */
@Service
public class UserService implements UserDetailsService {
    @Resource
    SecurityService securityService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        if (s == null || "".equals(s)) {
            throw new UsernameNotFoundException(s + " not exists");
        } else {
            User user = securityService.findByUsername(s);
            if (user == null) {
                throw new UsernameNotFoundException(s + " not exists");
            } else {
                return user;
            }
        }
    }
}
